package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserManagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;

import com.itheima.pojo.Role;
import com.itheima.pojo.User;

import com.itheima.service.UserManagementService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = UserManagementService.class)
@Transactional
public class UserManagementImpl implements UserManagementService {
    //注入DAO对象
    @Autowired
    private UserManagementDao userManagementDao;
    public void add(User user) {
        userManagementDao.add(user);
    }

    //用户分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);

        Page<User> page = userManagementDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //根据ID删除检查项
    public void deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = userManagementDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException();
        }
        userManagementDao.deleteById(id);
    }

    public void edit(User user) {
        userManagementDao.edit(user);

    }

    @Override
    public User findById(Integer id) {
        return (User) userManagementDao.findById(id);
    }

    public List<User> findAll() {
        return (List<User>)userManagementDao.findAll();
    }

    @Override
    public User findByUsername(String username) {
        return (User)userManagementDao;
    }
    //查询所有role
    @Override
    public List<Role> findAllRole() {
        List<Role> roles = userManagementDao.findAllRole();
        return roles;
    }
}
